package com.hncu.edu.info.manager.dao;

import com.hncu.edu.info.manager.domain.Student;

import java.util.ArrayList;

public class OtherStudentDao implements BaseStudentDao {
    //创建集合
    private static ArrayList<Student> arrayList = new ArrayList<>();

    // 静态代码块（写类中构造外），便于测试
    static {
        Student stu1 = new Student("hncu001", "张三", "21", "1999-11-11");
        Student stu2 = new Student("hncu002", "李四", "20", "2000-11-11");
        arrayList.add(stu1);
        arrayList.add(stu2);

    }

    //空参构造
    public OtherStudentDao() {

    }

    //添加学生
    public boolean addStudent(Student stu) {
        arrayList.add(stu);
        return true;
    }

    //查找学生
    public Student[] findAllStudent() {
        Student[] students = new Student[arrayList.size()];
        //将集合内的对象全部放到数组中去
        for (int i = 0; i < students.length; i++) {
            Student student = arrayList.get(i);
            students[i] = student;
        }
        return students;
    }

    //删除学生
    public void deleteStudentBySid(String sid) {
        //找到位置
        int Index = getIndex(sid);
//        //删除
//        arr[Index] = null;
        arrayList.remove(Index);
    }

    //根据学号找位置
    public int getIndex(String sid) {
        //假设集合中没有这个学号
        int Index = -1;
        //遍历集合
        for (int i = 0; i < arrayList.size(); i++) {
            Student stu = arrayList.get(i);
            if (stu!=null&&stu.getid().equals(sid)) {
                Index = i;
                break;
            }
        }
        return Index;
    }

    //修改学生
    public void updateStudent(Student stu) {
        //获取被修改学生学号
        String sid = stu.getid();
        //找到该学生的索引位置
        int Index = getIndex(sid);
        //添加新的学生对象进去
//        arr[Index] = stu;
        arrayList.set(Index,stu);
    }
}
